package com.org.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.org.model.Address;
import com.org.service.IAddressService;
import com.org.utils.servlet.ServletUtils;
import com.org.utils.string.CreateGUID;

/**
 *@Author:liangjilong
 *@Date:2014-3-30
 *@CopyRight:liangjilong
 *@Version:1.0
 */
@Controller
@RequestMapping 
@SuppressWarnings("all")
public class AddressController {
	
	@Resource
	private IAddressService addressService;

	/**
	 * @throws Exception 
	 */
	@RequestMapping(value="/getReceiptAddressList.do")
	public void getReceiptAddressList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		PrintWriter out = ServletUtils.setResponseWriter(request, response);
		
		String callBack = request.getParameter("callback");
		String buyerId = "a1c6233a904149198a77fef1cac46bb7";//currentMember.getBuyerId();// request.getParameter("buyerId");
		
		JSONObject result = new JSONObject();
		Map params = new HashMap();
		params.put("buyerId", buyerId);
		try {
			Map res = addressService.getReceiptAddressListByBuyerId(params);
			List<Map> list = (List<Map>) res.get("list");
			if (list.size() > 0) {
				result.put("total", res.get("total"));
				result.put("data", list);
			} else {
				result.put("total", 0);
				result.put("data", list);
			}
			 
			out.write(callBack + "(" + result.toString() + ")");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	 

	/**
	 * @throws IOException 
	 */
	@RequestMapping(value="/getReceiptAddressInfo.do")
	public void getReceiptAddressInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = ServletUtils.setResponseWriter(request, response);
		
		JSONObject result = new JSONObject();
		String receiptAddId = request.getParameter("receiptAddId");
		try {
			Address addr = addressService.getReceiptAddressInfoById(receiptAddId);
			String isDefault = addr.getIsDefault();
			String area = addr.getArea();
			String address = addr.getAddress();
			String mobile = addr.getMobile();
			String telephone = addr.getTelephone();
			String name = addr.getName();

			result.put("area", area);
			result.put("isDefault", isDefault);
			result.put("name", name);
			result.put("address", address);
			result.put("mobile", mobile);
			result.put("telephone", telephone);
			out.write(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @throws IOException 
	 */
	@RequestMapping(value="/addReceiptAddress.do")
	public void addReceiptAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		PrintWriter out = ServletUtils.setResponseWriter(request, response);
		
		JSONObject result = new JSONObject();
		
		String buyerId = "a1c6233a904149198a77fef1cac46bb7";//currentMember.getBuyerId();
		Address add = new Address();

		String isDefault = request.getParameter("hasSelf");
		String area = request.getParameter("area");
		String address = request.getParameter("address");
		String mobile = request.getParameter("mobile");
		String telephone = request.getParameter("telephone");
		String name = request.getParameter("receiptName");
		try {
			add.setId(CreateGUID.createGuId());
			add.setBuyerID(buyerId);
			add.setIsDefault(isDefault);
			add.setName(name);
			add.setTelephone(telephone);
			add.setLastModified(new Date());
			add.setAddress(address);
			add.setArea(area);
			add.setMobile(mobile);
			addressService.addReceiptAddress(add);

			result.put("result", "1");
			out.write(result.toString());
		} catch (Exception e) {
			try {
				result.put("result", "0");
				out.write(result.toString());
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * @throws IOException 
	 */
	@RequestMapping(value="/updateReceiptAddress.do")
	public void updateReceiptAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = ServletUtils.setResponseWriter(request, response);
		
		JSONObject result = new JSONObject();
		String buyerId = "a1c6233a904149198a77fef1cac46bb7";//currentMember.getBuyerId();
		String isDefault = request.getParameter("hasSelf");
		String receiptAddId = request.getParameter("Id");
		String areaId = request.getParameter("areaId");
		String area = request.getParameter("area");
		String address = request.getParameter("address");
		String mobile = request.getParameter("mobile");
		String telephone = request.getParameter("telephone");
		String name = request.getParameter("receiptName");

		Address add = new Address();
		try {
			add.setId(receiptAddId);
			add.setLastModified(new Date());
			add.setAreaId(areaId);
			add.setArea(area);
			add.setTelephone(telephone);
			add.setBuyerID(buyerId);
			add.setIsDefault(isDefault);
			add.setName(name);
			add.setTelephone(telephone);
			add.setLastModified(new Date());
			add.setAddress(address);
			add.setArea(area);
			add.setMobile(mobile);
			addressService.updateReceiptAddress(add);
			result.put("result", "1");
			out.write(result.toString());

		} catch (Exception e) {
			try {
				result.put("result", "0");
				out.write(result.toString());
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/deleteReceiptAddress.do")
	public void deleteReceiptAddress(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = ServletUtils.setResponseWriter(request, response);
		
		JSONObject result = new JSONObject();
		String id = request.getParameter("addrId");
		try {
			addressService.deleteReceiptAddress(id);
			result.put("result", "1");
			out.write(result.toString());
		} catch (Exception e) {
			try {
				result.put("result", "0");
				out.write(result.toString());
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
		}

	}

}
